<?php

namespace App\Http\Controllers\Frontend\Content;

use App\Http\Controllers\BaseController;
use App\Http\Controllers\Controller;
use App\Interfaces\ArticleInterface;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ArticleController extends BaseController
{

    protected ArticleInterface $article;
    public function __construct(ArticleInterface $article)
    {
        $this->article = $article;
    }
    public function getArticleList(Request $request): JsonResponse
    {
        $tenantId = $request->headers->get('tenantId');
        $keyword = $request->input('keyword');
        $cateId = $request->input('cateId');
        $page = $request->input('limit');
        $limit = $request->input('limit')?$request->input('limit'):10;
        $params['cateId'] = $cateId;
        $params['title'] = $keyword;

        $res = $this->article->getPage($tenantId,$params,$page,$limit);

        if (!$res){
            return $this->apiResult(['code'=>1,'message'=>'暂无数据！']);
        }
        return $this->apiResult(['code'=>0,'data'=>$res,'message'=>'SUCCESS']);
    }

    /**
     * @param Request $request
     * @return JsonResponse
     */
    public function getArticleDetail(Request $request): JsonResponse
    {
        $id = $request->input('id');
        $res = $this->article->getById($id);

        if (!$res){
            return $this->apiResult(['code'=>1,'message'=>'暂无数据！']);
        }
        return $this->apiResult(['code'=>0,'data'=>$res,'message'=>'SUCCESS']);
    }
}
